package kr.pfg.pfgTracer.view.list
{
	import display.Align;
	
	import flash.display.Sprite;
	
	import kr.pfg.pfgTracer.model.event.VListItemSelectEvent;
	import kr.pfg.pfgTracer.model.vo.EchoClient;

	public class VListItemContainer extends Sprite
	{
		private var arrItem: Array;
		
		public function VListItemContainer()
		{
			this.init();
		}
		private function init(): void
		{
			arrItem = new Array();
			
			this.setPosition();
			this.initChild();
		}
		private function setPosition(): void
		{			
			for( var i: int = 1; i < arrItem.length; i ++ )
			{
				arrItem[i].y = Align.getPosY( arrItem[i-1], 1 );	
			}
		}
		private function initChild(): void
		{
			for( var i: int = 0; i < arrItem.length; i ++ )
			{
				if( ! this.contains( arrItem[i] ) ) 	this.addChild( arrItem[i] );
			}
		}
		private var itemWidth: int;
		override public function set width(value:Number):void
		{
			itemWidth = value;
			for( var i: int = 0; i < arrItem.length; i ++ )
			{
				arrItem[i].width = value;
			}
		}
		override public function set height(value:Number):void
		{
		}
		public function addItem( data: EchoClient ): void
		{
			var item: VListItem = new VListItem();
			item.addEventListener( VListItemSelectEvent.VLIST_ITEM_CLICK, onItemSelectListener );
			item.setData( data );
			item.width = itemWidth;
			item.height = 20;
			arrItem.push( item );
			
			this.initChild();
			this.setPosition();
		}
		private function onItemSelectListener( e: VListItemSelectEvent ): void
		{
			for( var i: int = 0; i < arrItem.length; i ++ )
			{
				if( arrItem[i] != e.currentTarget )
					arrItem[i].unSelected();
			}
		}
	}
}